package com.bigfreakindeals.Common;

import java.io.InputStream;
import java.net.URI;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.net.Uri;
import android.util.Log;


public class Tools {
	public static String[] getStringArray(List<String> list){
		
		String[] newString = new String[list.size()];
		for (int i = 0;i< list.size();i++){
			newString[i] = list.get(i);
		}
		return newString;
		
		
	}
	
	public static double filterAmount(String amountinStr){
		
		double result = 0.00;
		
		String temp ;
		temp =  amountinStr.replace('$', ' ');
		result = Double.parseDouble(temp);
		return TwoDecimalRound(result);
		
	}
	
	private static double TwoDecimalRound(double d) {
	      try{
	            DecimalFormat twoDForm = new DecimalFormat("#.##");
	            return Double.valueOf(twoDForm.format(d));
	      }catch(NumberFormatException ex){  
	            //TODO: handle error
	      }
	      return 0;
	    }
	
	public static Drawable getDrawableSeparatorGeneric(){
	
		return getDrawableSeparator(new int[]{0xffffffff, 0xffffffff})	;
	}
	public static Drawable getDrawableSeparator(int[] colors){
		Drawable drawable = new GradientDrawable
		(GradientDrawable.Orientation.LEFT_RIGHT, colors);
		return drawable;
	}
	public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
		 
			int width = bm.getWidth();
			 
			int height = bm.getHeight();
			 
			float scaleWidth = ((float) newWidth) / width;
			 
			float scaleHeight = ((float) newHeight) / height;
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
			return resizedBitmap;

	}
	public String DateLongtoString(Long time){
		int Month, day, year, hourpm, am_pm,minutes;
		Calendar calendar = Calendar.getInstance();
		
		Date date = new Date(); 
		date.getTime();
		
		calendar.setTimeInMillis(date.getTime());
		
		Month = calendar.get(Calendar.MONTH)+1;
		day =  (calendar.get(Calendar.DAY_OF_MONTH));
		year = calendar.get(Calendar.YEAR);
		hourpm = calendar.get(Calendar.HOUR);
		minutes = calendar.get(Calendar.MINUTE);
		am_pm = calendar.get(Calendar.AM_PM);
		
			
		return ( ((Month < 10)?("0"+Month):Month)+"/"+((day < 10)?("0"+day):day)+"/"+year+" "+((hourpm < 10)?("0"+hourpm):hourpm)+":"+((minutes< 10)?("0"+minutes):minutes)+" "+(am_pm == 0?"AM":"PM"));
	
		
	}
	
	public static long DateStringtoLong(String date){
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");  
		
		int month = Integer.parseInt(date.substring(0,2));
		int day = Integer.parseInt(date.substring(3,5));
		int year = Integer.parseInt(date.substring(6,10));
		
		int hourOfDay = Integer.parseInt(date.substring(11,13));
		int minute = Integer.parseInt(date.substring(14,16));
		
		String am_pm = date.substring(17);
		if (am_pm.compareTo("PM") == 0){
			hourOfDay += 12;
		}
		
		Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT")); 
		c.set(year, month-1, day, hourOfDay, minute);
		c.getTimeInMillis();
			
	 
		
		return c.getTimeInMillis();
	}
	
	public static String getURL(int webservice, ArrayList<String> params){
		//System.out.println("GETURL: "+webservice+" "+params);
		String url;
		switch (webservice) {
		case Constant.GET_PAYPAL_IPN:{
			 url = Constant.PROD_DOMAIN+((Constant.WEBSERVICE_PAYPAL_IPN.
					replaceAll("%sessionid",params.get(0).toString()))).
					replaceAll("%dealid", params.get(1).toString()).
					replaceAll("%quantity", params.get(2).toString());
			//where:
			return url;
		}
		case Constant.GET_SETTINGSUPDATE:{
			url = Constant.PROD_DOMAIN+((Constant.WEBSERVICE_SETTINGS_NOTIFICATIONS.replaceAll("%sessionid",
					params.get(0).toString()))).replaceAll("%beans", params.get(1).toString()).
					replaceAll("%deals", params.get(2).toString());
			//where:
			return url;
		}
		case Constant.GET_MARKUSED:{
			url =  Constant.PROD_DOMAIN+((Constant.WEBSERVICE_MARKCOUPON_USED.replaceAll("%sessionid",params.get(0).toString()))).replaceAll("%ccode", params.get(1).toString());
			return url;
		}
		case Constant.GET_CLOSESTSCHOOL:{
			url =  Constant.PROD_DOMAIN+((Constant.WEBSERVICE_CLOSEST_SCHOOL.replaceAll("%lat",params.get(0).toString()))).replaceAll("%long", params.get(1).toString());
			return url;
		}case Constant.GET_SCHOOLLIST:
			return  Constant.PROD_DOMAIN+Constant.WEBSERVICE_SCHOOL_ATTR_APPROVED_ONLY;
		
		case Constant.GET_MYCOUPONS:{
			url =  Constant.PROD_DOMAIN+(Constant.WEBSERVICE_GETCOUPON.replaceAll("%sid",params.get(0).toString()));
			return url;
			}
		case Constant.GET_TODAYS_DEAL:{
			url =  Constant.PROD_DOMAIN+(Constant.WEBSERVICE_GETTODAYSDEALS.replaceAll("%univ",params.get(0).toString()));
			return url;
			//login
		}
		case Constant.GET_LOGIN:
			url =  Constant.PROD_DOMAIN+((Constant.WEBSERVICE_LOGIN_REQ.replaceAll("%email",params.get(0).toString()))).replaceAll("%pass", params.get(1).toString());
			return url;
		case Constant.GET_PAYBYCREDITCARD:
			return Constant.PROD_DOMAIN+Constant.WEBSERVICE_PAYBYCREDITCARD;
		case Constant.GET_FORGOTPASSWORD:
			return Constant.PROD_DOMAIN+Constant.WEBSERVICE_FORGOTPASSWORD;
		case Constant.GET_FAQ:
			return Constant.PROD_DOMAIN+Constant.WEBSERVICE_FAQ;
		case Constant.GET_LOGINWITHFACEBOOK:
			url  = Constant.PROD_DOMAIN+((Constant.WEBSERVICE_FACEBOOKLOGIN.replaceAll("%token",params.get(0).toString())));
			return url;
		case Constant.GET_SIGNUPWITHFACEBOOK:
			url =  Constant.PROD_DOMAIN+((Constant.WEBSERVICE_FACEBOOKSIGNUP.replaceAll("%token",params.get(0).toString()))).replaceAll("%school", params.get(1).toString());
			return url;
		case Constant.GET_SIGNUP:
			 url = Constant.PROD_DOMAIN+((Constant.WEBSERVICE_SIGNUP.replaceAll("%email",params.get(0).toString()))).replaceAll("%pass", params.get(1).toString()).replaceAll("%user", params.get(2).toString()).replaceAll("%lname", params.get(3).toString()).replaceAll("%schoolname", params.get(4).toString());
			return url;
		case Constant.GET_DISCUSSLIST:
            return Constant.PROD_DOMAIN+(Constant.WEBSERVICE_DISCUSSIONPAGE_GETCOMMENTS.replaceAll("%id",params.get(0).toString()));
		case Constant.GET_POSTCOMMENTS:
				url = Constant.PROD_DOMAIN+((Constant.WEBSERVICE_DISCUSSIONPAGE_POSTCOMMENTS.
					replaceAll("%session_token",params.get(0).toString()))).
					replaceAll("%id", params.get(1).toString()).
					replaceAll("%comment", params.get(2).toString()).
					replaceAll("%follow", params.get(3).toString());
				
				
				
			return url.replaceAll(" ", "%20");
		case Constant.GET_BEANSCOMMENT:
			
			return Constant.PROD_DOMAIN+(Constant.WEBSERVICE_GETBEANS_COMMENT);
				
		case Constant.GET_SETTINGSHANDLER:{
			url =  Constant.PROD_DOMAIN+((Constant.WEBSERVICE_SETTINGS_NOTIFICATIONS.replaceAll("%sessionid",params.get(0).toString()))).replaceAll("%beans", params.get(1).toString()).replaceAll("%deals", params.get(2).toString());
			
			 return url;
		}
		default:
			
			
			return null;
		}
	
		
		
	}
	
	public static int getIntValue(String value){
		int returnInt = 0;
		if (value !=null && value.length() >0){
			if (value.compareTo("0")  == 0){
				returnInt = Integer.parseInt(value);
			}else 
				returnInt = Integer.parseInt((value.startsWith("#")?value.substring(1,value.length()):value),16);
		}else {
		
		returnInt = 0;
		}
		
		return (returnInt);		
	}
	
	
	
	public static Drawable getDrawableTab(int[] colors,Orientation orientation){
	
		

		Drawable drawable = new GradientDrawable(orientation , colors);
		int stroke = 2;/*stroke width*/
		
		((GradientDrawable)drawable).setCornerRadii(new float[] {10,10,10,10,0,0,0,0});
		((GradientDrawable)drawable).setStroke(stroke, 0/*black*/);
		
		
		
		return drawable;		
	
	}
	
	public static String setTimeBreakDown(long time){
	StringBuffer strTime = new StringBuffer();
		long leftdays=0;
		long lefthours=0;
		long leftmins=0;
		long leftseconds=0;
		

			//leftdays = TimeUnit.MILLISECONDS.toDays(time);
			
			leftdays = time/Constant.MILLISECOND_IN_DAY;
			
			lefthours = (time/Constant.MILLISECOND_IN_HOUR) - (leftdays * Constant.HOURS_IN_DAY);
			
			leftmins = (time/Constant.MILLISECOND_IN_MINUTE) - 
				((leftdays * Constant.HOURS_IN_DAY * Constant.MINUTE_IN_HOUR) + 
				(lefthours * Constant.MINUTE_IN_HOUR));
			
			
			leftseconds = (time/Constant.MILLISECOND_IN_SECOND) - 
			((leftdays * Constant.HOURS_IN_DAY  * Constant.MINUTE_IN_HOUR * Constant.SECONDS_IN_MINUTE) + 
			(lefthours * Constant.MINUTE_IN_HOUR * Constant.SECONDS_IN_MINUTE) + 
			(leftmins * Constant.SECONDS_IN_MINUTE));
		
		
		if (leftdays > 0){
			strTime.append(leftdays+( leftdays>1 ? ("D"+" "):("D"+" ")));
		}
		
		strTime.append(lefthours+"H"+":");
		strTime.append(leftmins+"M"+":");
		strTime.append(leftseconds+"S");
		
		return strTime.toString()+"";
	}
	public static Drawable getDrawableButtonGeneric(int[] colors,int cornerRadius,Orientation orientation){
	
		
		Drawable drawable = new GradientDrawable(orientation , colors);
		int stroke = 2;/*stroke width*/
		
		((GradientDrawable)drawable).setCornerRadius((float)cornerRadius);
		((GradientDrawable)drawable).setStroke(stroke, 0/*black*/);
		
		return drawable;	
	
	}
	public static Drawable getDrawableButtonGeneric(int[] colors,int cornerRadius/*round corners*/){
		return getDrawableButtonGeneric(colors,cornerRadius,GradientDrawable.Orientation.TOP_BOTTOM);
		
	}
	
	
	
	
	

}
